/* 
* Copyright (c) Harry Pierson. 
*
* This source code is subject to terms and conditions of the Microsoft 
* Permissive License. A copy of the license can be found in the License.html 
* file at the root of this distribution. If you cannot locate the  Microsoft 
* Permissive License, please send an email to harry@devhawk.net. By using this 
* source code in any fashion, you are agreeing to be bound by the terms of the 
* Microsoft Permissive License.
* 
* You must not remove this notice, or any other, from this software.
*/

/*
 * this is a managed http handler to take the CodeDom tree returned by WikiParser 
 * and render it as HTML to output to the browser for test purposes.
*/

using System;
using System.Web;
using System.Collections.Specialized;
using System.CodeDom;
using System.CodeDom.Compiler;
using HawkWiki.WikiRenderer;


namespace HawkWiki.SampleWiki
{
	public class ShowCodeHandler : IHttpHandler
	{
		public void ProcessRequest(HttpContext context)
		{
			StringCollection refAssemblies = new StringCollection();
			string pageName = WikiHandler.GetWikiPageName(context);

			CodeNamespace ns = WikiHandler.GenerateWikiCode(
				pageName,
				WikiHandler.GetWikiClassname(context),
				refAssemblies);

			if (ns == null)
			{
				context.Response.Write("<HTML><BODY>No code created - wiki page not found</body></html>");
				return;
			}

			//Create a C# code generator object from the C# CodeProvider 
			ICodeGenerator icg = new Microsoft.CSharp.CSharpCodeProvider().CreateGenerator();

			context.Response.Write("<HTML><BODY><PRE>");
			CodeGeneratorOptions cgo = new CodeGeneratorOptions();
			cgo.BracingStyle = "C";
			icg.GenerateCodeFromNamespace(ns, context.Response.Output, cgo);
			context.Response.Write("</PRE></BODY></HTML>");
		}

		public bool IsReusable
		{
			get
			{
				return true;
			}
		}
	}
}
